package com.sqsf.controller;

import com.sqsf.javabean.User;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 显示用户列表
    @GetMapping("/list")
    public String listUsers(Model model) {
        List<User> users = userService.FindAllUser();
        model.addAttribute("users", users);
        return "userList";
    }

    // 显示添加用户表单
    @GetMapping("/add")
    public String showAddForm() {
        return "addUser";
    }

    // 处理添加用户表单提交
    @PostMapping("/add")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/user/list";
    }
}